﻿package su.storage.module;

/**
 * 仓库管理
 */
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.kuakao.core.dto.PagerDTO;
import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import su.storage.dto.StorageMaterialDto;
import su.storage.dto.StorageProductDto;
import su.storage.entity.StorageProduct;
import su.storage.entity.StorageMaterial;
import su.storage.service.StorageService;
import su.tool.SuDateUtils;
import su.tool.SuDoubleUtils;
import su.tool.SuIntUtils;
@Controller
@RequestMapping("/admin/storage")
public class StorageServlet {

	@Autowired
	private StorageService storageService;

	protected final Log log = LogFactory.getLog(getClass());
	//===================product begin==========
	/**
	 * 仓库产品 出入库记录 列表
	 * @param request
	 * @param s
	 * @param model
	 * @return
	 */
	@RequestMapping("storage_product_index")
	public String storageProductIndex(HttpServletRequest request,PagerDTO s, Model model){
		List<Map<String,String>> list = storageService.getStorageProductsByPage(s);
		model.addAttribute("list", list);
		model.addAttribute("_page", s);
		return "/maintain/storage/storage_product_index";
	}
	/**
	 * 仓库产品 出入库记录 详情
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("storage_product_detail")
	public String storageProductDetail(HttpServletRequest request, Model model) {
        int id = SuIntUtils.getInt(request.getParameter("id"));
        if(id==0){
        	model.addAttribute("errorCode", "请选择一条产品出入仓库的记录");
        	return "su_error";
        }
        
        StorageProduct baseinfo = storageService.getStorageProductById(id);
        model.addAttribute("baseinfo", baseinfo);
		return "/maintain/storage/storage_product_detail";
	}
	/**
	 * 到 添加/编辑 仓库产品 出入库记录 页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("to_edit_storage_product")
	public String toEditProduct(HttpServletRequest request, Model model) {
        int id = SuIntUtils.getInt(request.getParameter("id"));
        if(id > 0){
        	StorageProduct sp = storageService.getStorageProductById(id);
            model.addAttribute("baseinfo", sp);
        }
		return "/maintain/storage/storage_product_edit";
	}

	/**
	 * 添加 仓库产品 出入库记录
	 * @param request
	 * @param response
	 * @param cate
	 * @param model
	 */
	@RequestMapping("do_save_storage_product")
	public String doSaveProduct(HttpServletRequest request,HttpServletResponse response,StorageProductDto dto, Model model){
		String res = "";
		StorageProduct sp = dto.toEntity();
		try {
			storageService.saveStorageProduct(sp);
		} catch (Exception e) {
			log.error(e.getMessage());
			res = e.getMessage();
		}
		if(StringUtils.isNotBlank(res)){
			request.setAttribute("errorCode", res);
			return "su_error";
		}
		return "redirect:/admin/storage/storage_product_index.action";
	}
	/**
	 * 到 批量添加 仓库产品 出入库记录 页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("to_batch_edit_storage_product")
	public String toBatchEditProduct(HttpServletRequest request, Model model) {
        
		return "/maintain/storage/storage_product_batch";
	}

	/**
	 * 删除 仓库产品 出入库记录
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("do_remove_storage_product")
	public void doRemoveProduct(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		if(writer==null)return;
		Map<String,String> msgs = new HashMap<String,String>();
		
		int id = SuIntUtils.getInt(request.getParameter("id"));
		
		if(id==0){
			msgs.put("no id", "请选择要删除的数据");
		}else{
			String msg = "";
			try {
				storageService.removeStorageProduct(id);
			} catch (Exception e) {
				log.error(e.getMessage());
				msg = e.getMessage();
			}
			
			if(StringUtils.isNotBlank(msg)){
				msgs.put("service error", msg);
			}else{
				msgs.put("success", "success");
			}
		}
		
		writer.print(JSONObject.fromObject(msgs));
		writer.flush();
		writer.close();
    }

	//===================product end==========
	//===================material begin==========
	/**
	 * 仓库原料 出入库记录 列表
	 * @param request
	 * @param s
	 * @param model
	 * @return
	 */
	@RequestMapping("storage_material_index")
	public String storageMaterialIndex(HttpServletRequest request,PagerDTO s, Model model){
		List<Map<String,String>> list = storageService.getStorageMaterialsByPage(s);
		model.addAttribute("list", list);
		model.addAttribute("_page", s);
		return "/maintain/storage/storage_material_index";
	}
	/**
	 * 仓库原料 出入库记录 详情
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("storage_material_detail")
	public String storageMaterialDetail(HttpServletRequest request, Model model) {
        int id = SuIntUtils.getInt(request.getParameter("id"));
        if(id==0){
        	model.addAttribute("errorCode", "请选择一条原料出入仓库的记录");
        	return "su_error";
        }
        
        StorageMaterial baseinfo = storageService.getStorageMaterialById(id);
        model.addAttribute("baseinfo", baseinfo);
		return "/maintain/storage/storage_material_detail";
	}
	/**
	 * 到 添加/编辑 仓库原料 出入库记录 页面
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("to_edit_storage_material")
	public String toEditMaterial(HttpServletRequest request, Model model) {
        int id = SuIntUtils.getInt(request.getParameter("id"));
        if(id > 0){
        	StorageMaterial baseinfo = storageService.getStorageMaterialById(id);
            model.addAttribute("baseinfo", baseinfo);
        }
		return "/maintain/storage/storage_material_edit";
	}

	/**
	 * 添加 仓库原料 出入库记录
	 * @param request
	 * @param response
	 * @param cate
	 * @param model
	 */
	@RequestMapping(value="do_save_storage_material",method=RequestMethod.POST)
	public String doSaveMaterial(HttpServletRequest request,HttpServletResponse response, Model model,StorageMaterialDto dto){
		String res = "";
		StorageMaterial sm = dto.toEntity();
		try {
			storageService.saveStorageMaterial(sm);
		} catch (Exception e) {
			log.error(e.getMessage());
			res = e.getMessage();
		}
		if(StringUtils.isNotBlank(res)){
			request.setAttribute("errorCode", res);
			return "su_error";
		}
		return "redirect:/admin/storage/storage_material_index.action";
	}	

	/**
	 * 删除 仓库原料 出入库记录
	 * @param request
	 * @param response
	 * @param model
	 */
	@RequestMapping("do_remove_storage_material")
	public void doRemoveMaterial(HttpServletRequest request,HttpServletResponse response, Model model){
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8");
        PrintWriter writer = null;
		try {
			writer = response.getWriter();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
		if(writer==null)return;
		Map<String,String> msgs = new HashMap<String,String>();
		
		int id = SuIntUtils.getInt(request.getParameter("id"));
		
		if(id==0){
			msgs.put("no id", "请选择要删除的数据");
		}else{
			String msg = "";
			try {
				storageService.removeStorageMaterial(id);
			} catch (Exception e) {
				log.error(e.getMessage());
				msg = e.getMessage();
			}
			
			if(StringUtils.isNotBlank(msg)){
				msgs.put("service error", msg);
			}else{
				msgs.put("success", "success");
			}
		}
		
		writer.print(JSONObject.fromObject(msgs));
		writer.flush();
		writer.close();
    }
	//===================material end==========
	
}
